const http = require("../../utils/http.js")
const pageInfo = {
    page: 1,
    limit: 5,
    pages: 0,
    categoryId: ""
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        baseImage: http.baseUrl,
        activeKey: 0,
        categoryList: [],
        bookList: [],
        pageInfo: {
            ...pageInfo
        }
    },
    getCategoryList() {
        http.get('wx/v1/book/category/list').then(data => {
            this.setData({
                categoryList: [...data]
            })
        })
    },
    changeTag(e) {
        const categoryId = e.currentTarget.dataset.categoryId
        this.setData({
            pageInfo: {
                ...pageInfo,
                categoryId
            },
            bookList: []
        })
        if (categoryId) {
            this.getCategoryBookPage()
        } else {
            this.getBookPage();
        }
    },
    getBookPage() {
        http.get('wx/v1/book/page', this.data.pageInfo).then(data => {
            this.setData({
                bookList: [...this.data.bookList, ...data.records],
                "pageInfo.pages": data.pages
            })
        })
    },
    getCategoryBookPage() {
        http.get('wx/v1/book/category/page', this.data.pageInfo).then(data => {
            this.setData({
                bookList: [...this.data.bookList, ...data.records],
                "pageInfo.pages": data.pages
            })
        })
    },
    scrollToBottom() {
        if (this.data.pageInfo.page < this.data.pageInfo.pages) {
            this.setData({
                "pageInfo.page": this.data.pageInfo.page + 1
            })
            if (this.data.pageInfo.categoryId) {
                this.getCategoryBookPage()
            } else {
                this.getBookPage()
            }
        } else {
            wx.showToast({title: "没有更多图书！"})
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getCategoryList()
        this.getBookPage()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})
